package zb.blog.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zb.blog.base.bean.Result;
import zb.blog.base.global.GlobalVar;
import zb.blog.base.sessionScope.SessionScope;
import zb.blog.base.util.EncryptUtil;
import zb.blog.bean.AuthPersonBean;
import zb.blog.bean.AuthUserBean;
import zb.blog.dao.LoginDao;
import zb.blog.service.ILoginService;
import zb.blog.service.IVerificationCode;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.Map;

@Service
public class LoginService implements ILoginService {
    @Autowired
    private LoginDao loginDao;
    @Autowired
    private IVerificationCode iVerificationCode;

    @Override
    public Result login(Map<String, String> param, HttpSession session) {
        Result r = new Result();
        //验证码校验
        int status = iVerificationCode.validateCode(session, param);
        if (status == 1) {
            String username = param.get("username");
            String password = param.get("password");
            if (username == null || username.length() <= 0 || password == null || password.length() <= 0) {
                r.setMsg("用户名或者密码不能为空！");
                r.setSuccess(GlobalVar.FAIL);
                return r;
            }
            Map<String, Object> userMap = loginDao.loginByUsername(username);
            if (userMap != null && EncryptUtil.md5Base64(password).equals(userMap.get("password"))) {
                AuthPersonBean ap = this.transformMapToBean(userMap);
                session.setAttribute(GlobalVar.LOGIN_USER, ap);
                r.setSuccess(GlobalVar.SUCCESS);
                r.setMsg("登录成功！");
                return r;
            }
            r.setSuccess(GlobalVar.FAIL);
            r.setMsg("用户名或密码不匹配！");
        } else {
            r.setSuccess(GlobalVar.FAIL);
            r.setMsg("验证码失效！");
        }
        return r;
    }

    @Override
    public Boolean exit() {
        return SessionScope.removeUser();
    }

    @Override
    public boolean checkLogined() {
        AuthPersonBean authPersonBean = SessionScope.getLoginAuth();
        if (authPersonBean != null) {
            return true;
        }
        return false;
    }

    private AuthPersonBean transformMapToBean(Map<String, Object> userMap) {
        AuthUserBean au = new AuthUserBean();
        au.setId((Integer) userMap.get("id"));
        au.setUsername((String) userMap.get("username"));
        au.setCreate_time((Timestamp) userMap.get("create_time"));
        au.setStatus((Integer) userMap.get("status"));

        AuthPersonBean ap = new AuthPersonBean();
        ap.setUser(au);
        ap.setAddress((String) userMap.get("address"));
        ap.setEmail((String) userMap.get("email"));
        ap.setHeader_img(GlobalVar.QNY_SITE + "/" + (String) userMap.get("header_img"));
        ap.setNickname((String) userMap.get("nickname"));
        ap.setProvince_id((Integer) userMap.get("province_id"));
        ap.setCity_id((Integer) userMap.get("city_id"));
        ap.setArea_id((Integer) userMap.get("area_id"));
        return ap;

    }
}
